package www.xpu.edu.app.student;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import www.xpu.edu.app.R;
import www.xpu.edu.app.Stu12;
import www.xpu.edu.app.Stu13;
import www.xpu.edu.app.Stu13_Adpter;

public class Activity_stu_13 extends AppCompatActivity {
    //投递管理:查看该学生投递出的所有简历
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_13);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        ListView list13=findViewById(R.id.list_stu13);
        ArrayList<Stu13> list=new ArrayList<>();

        //去重
        HashSet<Stu13> list2=new HashSet<>();
        list2.add(new Stu13("某公司","2k","工程师","今天"));

        Button button1=findViewById(R.id.title_back);
        Button button2=findViewById(R.id.title_change);
        TextView text_title=findViewById(R.id.title_text);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button2.setVisibility(View.INVISIBLE);
        text_title.setText("投递管理");



        SharedPreferences pref=getSharedPreferences("num",MODE_PRIVATE);
        String num=pref.getString("num","");
        sendRequest(num);
        SharedPreferences pref2=getSharedPreferences("stu13",MODE_PRIVATE);
        String data=pref2.getString("data","");

        Gson gson=new Gson();
        //有些用户真的没有收到邀约或者投递简历，空白太难看了，后面的功能也无法展示，所以添加一个测试数据
        list=gson.fromJson(data,new TypeToken<List<Stu13>>(){}.getType());

        if(list==null)
        {
            //自欺欺人罢了，获取不到数据则不停的刷新，不崩溃才怪呢
            startActivity(new Intent(Activity_stu_13.this,Activity_stu_13.class));
        }
        for (int i = 0; i < (list != null ? list.size() : 0); i++) {
            //构造函数 参数顺序 String jobname, String weap, String address, String time
            //list2是哈希表，可以达到去重的效果，数据库的查询语句有问题，有过多的重复性，不得已而为之！！！！！
           list2.add(new Stu13(list.get(i).getJobname(),list.get(i).getWeap(),list.get(i).getAddress(),list.get(i).getTime()));
        }
        System.out.println(list2.toString());
        ArrayList<Stu13> list3=new ArrayList<>();
        //迭代器取出list2的数据
        Iterator iterator=list2.iterator();
        while(iterator.hasNext())
        {
            list3.add((Stu13)iterator.next());
        }
        Stu13_Adpter adpter=new Stu13_Adpter(Activity_stu_13.this,R.layout.adapterjob13, list3);
        list13.setAdapter(adpter);
    }
    //测试用户:19829088780
    //http://188.131.250.4:8080/xpu//Select_stu_resume?num=19829088780
    private void sendRequest(final String num)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            /*http://192.168.43.188:8080/test2/resume*/
                            /*http://192.168.123.196:8080/test2/resume*/
                            //.url("http://192.168.43.188:8080/test2/resume")
                            .url("http://188.131.250.4:8080/xpu/Select_stu_resume?num="+num)
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    SharedPreferences.Editor editor=getSharedPreferences("stu13",MODE_PRIVATE).edit();
                    // System.out.println("job3的内容【服务器端获取】->"+responseData);
                    editor.putString("data",responseData);
                    editor.apply();



                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
}

